<?php

class Payment_Item_Campaign extends Payment_Item_Abstract
{
	const CODE = 12;	
	protected $_code = self::CODE;
	private $_is_valid = array();
	private $_has_record = false;
	private $_price = 0;
	private $_paid_price = 0;
	
	public function getDefaultTime()
	{
		return self::ONE_MONTH;
	}
	
	public function hasRecord($id_page)
	{
		$this->isValid($id_page);
		return $this->_has_record;
	}
	
	public function isValid($id_page)
	{
		if (isset($this->_is_valid[$id_page]))
		{
			return $this->_is_valid[$id_page];
		}

		$pageItemsModel = Payment_Model_DbTable_PageItems::getInstance();
		$pageItem = $pageItemsModel->findByPageItem($id_page, self::CODE);

		$this->_is_valid[$id_page] = false;

		if (!empty($pageItem))
		{
			$this->_has_record = true;
			if (intval($pageItem->time_expired) - time() > 0)
			{
				$this->_is_valid[$id_page] = true;
			}
		}
		else
		{
			$this->_is_valid[$id_page] = false;
		}

		return $this->_is_valid[$id_page];
	}
	
	public function getPrice($fan_count)
	{
		if (empty($this->_price))
		{
			$metadataTable = Payment_Model_DbTable_Metadata::getInstance();
			$priceList = $metadataTable->getDecodedValue(Payment_Model_DbTable_Metadata::PRICE_TABLE);

			foreach ($priceList as $fans => $price)
			{
				if ($fan_count <= $fans)
				{
					return $this->_price = $price;
				}
			}
		}
		return $this->_price;
	}
	
	public function getPaidPrice($id_page)
	{
		if (empty($this->_paid_price))
		{
			$orderTable = Payment_Model_DbTable_Orders::getInstance();
			$totalRow = $orderTable->getTotal($id_page, self::CODE);
			if (!empty($totalRow))
			{
				$this->_paid_price = $totalRow->total;
			}
		}
		return $this->_paid_price;
	}
}